Component({
  properties: {
    cartItems: {
      type: Array,
      value: []
    },
    totalQuantity: {
      type: Number,
      value: 0
    },
    totalPrice: {
      type: Number,
      value: 0
    }
  },

  data: {
    showPanel: false
  },

  methods: {
    // 切换购物车面板
    togglePanel() {
      this.setData({
        showPanel: !this.data.showPanel
      });
    },

    // 关闭购物车面板
    closePanel() {
      this.setData({
        showPanel: false
      });
    },

    // 清空购物车
    clearCart() {
      wx.showModal({
        title: '提示',
        content: '确定要清空购物车吗？',
        success: (res) => {
          if (res.confirm) {
            this.triggerEvent('clearCart');
            this.closePanel();
          }
        }
      });
    },

    // 增加商品数量
    increaseQuantity(e) {
      const { id } = e.currentTarget.dataset;
      this.triggerEvent('changeQuantity', { id, type: 'increase' });
    },

    // 减少商品数量
    decreaseQuantity(e) {
      console.log("decrease");
      const { id } = e.currentTarget.dataset;
      this.triggerEvent('changeQuantity', { id, type: 'decrease' });
    },

    // 去结算
    checkout() {
      if (this.data.totalQuantity === 0) {
        wx.showToast({
          title: '请先选择商品',
          icon: 'none'
        });
        return;
      }
      // 跳转到结算页面
      wx.navigateTo({
        url: '/pages/menu/checkout/index'
      });
    }
  }
}) 